package com.lxh.model;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;

import android.content.ContentProviderOperation;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.OperationApplicationException;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.RemoteException;
import android.provider.ContactsContract;
import android.util.Log;

public class Utils {
	
	public static HashMap<String,String> getPrefixCodePairs(){
		HashMap<String,String> mapResults = new HashMap<String, String>();
		
		mapResults.put("0918", "098");
		mapResults.put("0919", "097");
		mapResults.put("0986", "096");
		mapResults.put("0985", "095");
		
		return mapResults;
	}

	public static ArrayList<String> fetchContacts(ContentResolver contentResolver) {
		ArrayList<String> results = new ArrayList<String>();
		
		Cursor cursor = contentResolver.query(StaticVariables.CONTENT_URI, null, null, null, null);

		// Loop for every contact in the phone
		if (cursor.getCount() > 0) {
			
			while (cursor.moveToNext()) {
				String contact_id = cursor.getString(cursor.getColumnIndex(StaticVariables._ID));
//				String name = cursor.getString(cursor.getColumnIndex(StaticVariables.DISPLAY_NAME));

				int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(StaticVariables.HAS_PHONE_NUMBER)));

				if (hasPhoneNumber > 0) {
					results.add(contact_id);
				}
			}
		}
		
		return results;
	}
	
	public static ArrayList<String> retrieveContactNumbers(ContentResolver contentResolver, String contactId){
		ArrayList<String> results = new ArrayList<String>();
		
		Cursor phoneCursor = contentResolver.query(
				StaticVariables.PhoneCONTENT_URI, null, StaticVariables.Phone_CONTACT_ID + " = ?",
				new String[] { contactId }, null);

		String phoneNumber;
		while (phoneCursor.moveToNext()) {											
			
			phoneNumber = phoneCursor.getString(phoneCursor
					.getColumnIndex(StaticVariables.NUMBER));
			results.add(phoneNumber);
			Log.e("UpdatePrefixCode", "getted number: " + phoneNumber);
		}
		phoneCursor.close();
		
		return results;
	}
	
	private Bitmap retrieveContactPhoto(ContentResolver contentResolver, String contactId) {
		 
        Bitmap photo = null;
 
        try {
            InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(contentResolver,
                    ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(contactId)));
 
            if (inputStream != null) {
                photo = BitmapFactory.decodeStream(inputStream);
            }
 
            assert inputStream != null;
            inputStream.close();
 
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        return photo;
    }

	public static boolean updateContactNumber(ContentResolver contentResolver, String contactId, String newPhoneNumber,
			String oldPhoneNumber){
		Log.e("UpdatePrefixCode", "UpdatecontactNumber----" + contactId + "---" + newPhoneNumber + "=====" + oldPhoneNumber);
		boolean result = false;
		
		ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
		String where = ContactsContract.Data.CONTACT_ID 
//				+ " = ? AND "				
//				+ String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE)
				+ " = ? AND "
				+ ContactsContract.CommonDataKinds.Phone.NUMBER + " = ?";
		
		String[] params = new String[] {contactId, oldPhoneNumber};
		
		
		ops.add(ContentProviderOperation
				.newUpdate(ContactsContract.Data.CONTENT_URI)
				.withSelection(where, params)
				.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, newPhoneNumber)
				.build());
		try{
			contentResolver.applyBatch(ContactsContract.AUTHORITY, ops);	
			result = true;
		}catch(Exception e){
			Log.e("UpdatePrefixCode", ""+e);
			result = false;
		}
		
		return result;
	}
}
